<template>
	<a-drawer
		:title="formData.id ? '编辑' : '增加'"
		:width="500"
		:visible="visible"
		:destroy-on-close="true"
		:body-style="{ paddingBottom: '80px' }"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="规则名称：" name="title">
				<a-input v-model:value="formData.title" placeholder="请输入规则名称" allow-clear />
			</a-form-item>
			<div>
				<div class="ant-form-item-label block">活动日期：</div>
				<a-space>
					<a-form-item name="startTime">
						<a-date-picker format="YYYY-MM-DD" v-model:value="formData.startTime"></a-date-picker>
					</a-form-item>
					<a-form-item >
						至
					</a-form-item>
					<a-form-item name="endTime">
						<a-date-picker format="YYYY-MM-DD" v-model:value="formData.endTime"></a-date-picker>
					</a-form-item>
				</a-space>
			</div>
			<a-form-item label="充值金额：" name="perLimitCount">
				<a-input-number v-model:value="formData.perLimitCount" style="width: 180px;" :min="1" placeholder="请输入充值金额" />
			</a-form-item>
			<a-form-item label="赠送类型：" name="presentType">
				<a-select
					:disabled="formData.id"
					v-model:value="formData.presentType"
					:options="presentTypeOptions"
					placeholder="赠送类型"></a-select>
			</a-form-item>
			<a-form-item v-if="formData.presentType === 'AMOUNT' || formData.presentType === 'POINT'" label="赠送金额/积分/券：" name="presentValue">
				<a-input-number
					v-model:value="formData.presentValue"
					style="width: 160px;"
					:min="0"
					placeholder="请输入券面额">
					<template v-if="formData.presentType === 'AMOUNT' || formData.presentType === 'POINT'" #addonAfter>
						{{ formData.presentType === 'AMOUNT' ? '元' : '积分' }}
					</template>
				</a-input-number>
			</a-form-item>
			<a-form-item label="总量限制：" name="totalLimitType">
				<a-radio-group
					v-model:value="formData.totalLimitType"
					:options="totalLimitTypeOptions"></a-radio-group>
			</a-form-item>
			<a-form-item label="每人限购：" name="userLimitType">
				<a-radio-group
					v-model:value="formData.userLimitType"
					:options="userLimitTypeOptions"></a-radio-group>
			</a-form-item>
			<a-form-item label="规则描述：" name="notes">
				<a-input v-model:value="formData.notes" placeholder="请输入详细规则说明"/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
		</template>
		<Icon-selector ref="iconSelector" @iconCallBack="iconCallBack" />
	</a-drawer>
</template>

<script setup name="crmVoucherRuleForm">
	import { message } from 'ant-design-vue'
	import dayjs from "dayjs"
	import {required, requiredDateRange} from '@/utils/formRules'
	import IconSelector from '@/components/Selector/iconSelector.vue'
	import crmApi from '@/api/crm/crmApi'
	import { getCurrentInstance } from 'vue'
	import tool from "@/utils/tool";
	// 默认是关闭状态
	let visible = $ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const formData = ref({})
	const submitLoading = ref(false)
	const iconSelector = ref()
	// 打开抽屉
	const onOpen = (record) => {
		visible = true
		formData.value = {
			status: 0,
			totalLimitType: '0',
			userLimitType: '0',
			presentType: 'AMOUNT'
		}
		if (record) {
			formData.value = Object.assign({}, record)
			if (formData.value.startTime) {
				formData.value.startTime = dayjs(formData.value.startTime)
			}
			if (formData.value.endTime) {
				formData.value.endTime = dayjs(formData.value.endTime)
			}
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible = false
	}

	// 默认要校验的
	const formRules = {
		title: [required('请输入规则名称')],
		perLimitCount: [required('请输入充值金额')],
		presentType: [required('请选择赠送类型')],
		presentValue: [required('请输入赠送值')],
		startTime: [required('请输入活动日期')],
		endTime: [required('请输入活动日期')]
	}

	// 图标选择器回调
	const iconCallBack = (value) => {
		formData.value.icon = value
	}
	const { proxy } = getCurrentInstance()
	let presentTypeOptions = proxy.$TOOL
		.dictTypeList('VOUCHER_RULE_PRESENT_TYPE')
		.map((item) => {
			return {
				value: item['dictValue'],
				label: item['name']
			}
		})
	let totalLimitTypeOptions = [
		{ label: '不限制', value: '0' },
		{ label: '总共', value: '1' },
		{ label: '每天', value: '2' }
	]
	let userLimitTypeOptions = [
		{ label: '不限制', value: '0' },
		{ label: '总共', value: '1' },
		{ label: '每天', value: '2' }
	]
	let storeOptions = tool
		.tableList('ORG_CATEGORY')
		.filter(item => item.category === 'STORE')
		.map(item => {
			return {
				value: item['code'],
				label: item['name']
			}
		})
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				const param = parameterChanges(formData.value)
				if (!param.startTime || !param.endTime) {
				}
				param.startTime = dayjs(param.startTime).format('YYYY-MM-DD')
				param.endTime = dayjs(param.endTime).format('YYYY-MM-DD')
				submitLoading.value = true
				crmApi.voucherRuleSubmitForm(param, param.id).then(() => {
					visible = false
					emit('successful')
				}).finally(() => {
					submitLoading.value = false
				})
			})
	}
	const parameterChanges = (data) => {
		return Object.assign({}, data)
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
